from itertools import accumulate
from typing import List


class Solution:
    def maxRotateFunction(self, nums: List[int]) -> int:
        if len(nums) < 2:
            return 0

        allSum = sum(nums)
        curr = 0
        for i in range(len(nums)):
            curr += i * nums[i]
        ans = curr
        
        for i in range(1, len(nums)):
            curr += allSum - len(nums) * nums[-i]
            ans = max(ans, curr) 
        return ans

    def maxRotateFunction2(self, nums: List[int]) -> int:
        n, s = len(nums), sum(nums)
        return max(accumulate(reversed(nums), lambda a,b: a+s-n*b, initial=sum(i * nums[i] for i in range(n))))